<template>
  <div>
    <div class="search">
<!--      <el-input placeholder="请输入工人姓名" style="width: 200px" v-model="workerName"></el-input>-->
<!--      <el-date-picker-->
<!--          v-model="attendanceMonth"-->
<!--          type="month"-->
<!--          placeholder="选择考勤月份"-->
<!--          style="width: 180px"-->
<!--      ></el-date-picker>-->
<!--      <el-button type="info" plain style="margin-left: 10px" @click="search">查询</el-button>-->
<!--      <el-button type="warning" plain style="margin-left: 10px" @click="reset">重置</el-button>-->
      <el-button type="primary" plain style="margin-left: 10px" @click="exportExcel">导出</el-button>
    </div>


    <div class="table">
      <el-table :data="tableData" stripe border>
        <el-table-column label="序号" align="center" width="55">
          <template slot-scope="scope">{{ scope.$index + 1 }}</template>
        </el-table-column>
        <el-table-column prop="workerName" label="工人姓名" width="100"></el-table-column>
        <el-table-column label="工作时长" width="auto">
          <template slot-scope="scope">
            <div class="day-duration-container">
              <div class="day-duration" v-for="(day, index) in scope.row.days" :key="index">
                <div class="day">{{ index + 1 }}</div>
                <div class="duration">{{ day }}</div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="合计" align="center" width="100">
          <template slot-scope="scope">
            {{ scope.row.days.reduce((acc, cur) => acc + cur, 0) }}
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-button @click="prevPage" icon="el-icon-arrow-left" plain></el-button>
        <el-input v-model.number="currentPage" type="number" min="1" :max="totalPages" style="width: 80px"></el-input>
        <span>/ {{ totalPages }}</span>
        <el-button @click="nextPage" icon="el-icon-arrow-right" plain></el-button>
      </div>
    </div>

  </div>
</template>

<script>
import FileSaver from "file-saver";
import XLSX from "xlsx";

export default {
  name: "Attendance",
  data() {
    return {
      tableData: [],
      pageNum: 1,
      pageSize: 10,
      total: 0,
      totalPages: 1,
      workerName: null,
      attendanceMonth: null
    };
  },
  computed: {
    currentPage: {
      get() {
        return this.pageNum;
      },
      set(val) {
        this.pageNum = val;
      }
    }
  },
  created() {
    this.totalPages = Math.ceil(this.total / this.pageSize);
    this.load(1);
  },
  methods: {
    load(pageNum) {
      if (pageNum) this.pageNum = pageNum;
      // Simulated data generation
      const data = [
        { workerName: "张三", days: Array(31).fill(0) },
        { workerName: "李四", days: Array(31).fill(0) },
        { workerName: "王五", days: Array(31).fill(0) },
        { workerName: "赵六", days: Array(31).fill(0) }
      ];
      // Randomly allocate work hours for each person, then fix them
      data[0].days = this.generateFixedWorkHours(22, 196);
      data[1].days = this.generateFixedWorkHours(25, 210);
      data[2].days = this.generateFixedWorkHours(25, 198);
      data[3].days = this.generateFixedWorkHours(27, 200);
      // Update tableData
      this.tableData = data;
    },
    generateFixedWorkHours(workDays, totalHours) {
      const result = [];
      let total = 0;
      for (let i = 0; i < 30; i++) {
        if (i < workDays - 1) {
          const hours = Math.floor(Math.random() * 8) + 1; // Randomly generate work hours from 1 to 8
          result.push(hours);
          total += hours;
        } else {
          result.push(0); // Fill days not worked with 0
        }
      }
      result.push(totalHours - total); // Ensure total work hours equals the specified value on the last day
      return result;
    },
    prevPage() {
      if (this.pageNum > 1) {
        this.load(this.pageNum - 1);
      }
    },
    nextPage() {
      if (this.pageNum < this.totalPages) {
        this.load(this.pageNum + 1);
      }
    },
    reset() {
      this.workerName = null;
      this.attendanceMonth = null;
      this.load(1);
    },
    search() {
      this.load(1);
    },
    exportExcel() {
      const data = this.tableData.map(row => ({
        "工人姓名": row.workerName,
        "工作时长": row.days.join(", ")
      }));

      const ws = XLSX.utils.json_to_sheet(data);
      const wb = XLSX.utils.book_new();
      XLSX.utils.book_append_sheet(wb, ws, "Sheet1");

      const excelTitle = "考勤表";
      const wbout = XLSX.write(wb, { bookType: "xlsx", bookSST: true, type: "array" });

      try {
        FileSaver.saveAs(
            new Blob([wbout], { type: "application/octet-stream" }),
            excelTitle + ".xlsx"
        );
      } catch (e) {
        console.error(e);
      }
    }

  }
};
</script>

<style scoped>
.search {
  margin-bottom: 20px;
}


.table {
  overflow-x: auto;
}

.day-duration-container {
  display: flex;
  flex-direction: row;
  align-items: center;
  overflow-x: auto;
  white-space: nowrap;
}

.day-duration {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 60px; /* Adjust width */
}

.day,
.duration {
  width: 100%; /* Ensure full width */
  height: 30px;
  line-height: 30px;
  text-align: center;
  border: 1px solid #ebeef5;
}

.pagination {
  margin-top: 20px;
  text-align: center;
}
</style>
